﻿using System;
using System.Runtime.InteropServices;
using System.Security;

namespace Co9320.NamaAlert.Extension.Model
{
    class Setting : ISetting
    {
        private IPluginSettings setting;

        public Setting(IPluginSettings setting)
        {
            this.setting = setting;

            fromMailAddress = setting.GetString("FromMailAddress");
            toMailAddress = setting.GetString("ToMailAddress");
            smtpPort = (int)setting.GetInteger("SmtpPort", 25);

            textEncoding = setting.GetString("TextEncoding");
            if (textEncoding != "Shift_JIS" && textEncoding != "UTF-8" && textEncoding != "ISO-2022-JP")
            {
                textEncoding = "ISO-2022-JP";
            }

            smtpServer = setting.GetString("SmtpServer");
            smtpUserName = setting.GetString("SmtpUserName");

            smtpPassword = new SecureString();
            foreach (char c in setting.GetString("SmtpPassword","").ToCharArray())
                smtpPassword.AppendChar(c);
        }

        public IPluginSettings PluginSettings
        {
            get { return setting; }
        }

        /// <summary>
        /// 送信元メールアドレス
        /// </summary>
        public string FromMailAddress
        {
            get { return fromMailAddress; }
            set 
            {
                fromMailAddress = value;
                setting.SetString("FromMailAddress", fromMailAddress);
            }
        }

        /// <summary>
        /// 送信先メールアドレス
        /// </summary>
        public string ToMailAddress
        {
            get { return toMailAddress; }
            set 
            {
                toMailAddress = value;
                setting.SetString("ToMailAddress", toMailAddress);
            }
        }

        /// <summary>
        /// 文字エンコーディング
        /// </summary>
        public string TextEncoding
        {
            get { return textEncoding; }
            set
            {
                var textEncoding = value;
                if (textEncoding != "Shift_JIS" && textEncoding != "UTF-8" && textEncoding != "ISO-2022-JP")
                {
                    textEncoding = "ISO-2022-JP";
                }
                setting.SetString("TextEncoding", textEncoding);
            }
        }

        /// <summary>
        /// SMTPサーバーアドレス
        /// </summary>
        public string SmtpServer
        {
            get { return smtpServer; }
            set 
            {
                smtpServer = value;
                setting.SetString("SmtpServer", smtpServer);
            }
        }

        /// <summary>
        /// SMTPサーバーポート
        /// </summary>
        public int SmtpPort
        {
            get { return smtpPort; }
            set {
                smtpPort = value;
                setting.SetInteger("SmtpPort", smtpPort);
            }
        }

        /// <summary>
        /// SMTPでユーザー認証を行うかどうか
        /// </summary>
        public bool SmtpUseAuth
        {
            get { return (SmtpUserName != "" && SmtpPassword.Length != 0); }
        }

        /// <summary>
        /// SMTP認証ユーザー名
        /// </summary>
        public string SmtpUserName
        {
            get { return smtpUserName; }
            set {
                smtpUserName = value;
                setting.SetString("SmtpUserName", smtpUserName);
            }
        }

        /// <summary>
        /// SMTP認証パスワード
        /// </summary>
        public SecureString SmtpPassword
        {
            get { return smtpPassword; }
            set
            {
                smtpPassword = value;
                IntPtr pass = Marshal.SecureStringToBSTR(smtpPassword);
                setting.SetString("SmtpPassword", Marshal.PtrToStringBSTR(pass));
            }
        }

        /// <summary>
        /// SMTPでSSLを使うかどうか
        /// </summary>
        public bool SmtpUseSsl
        {
            get { return smtpUseSsl; }
            set {
                smtpUseSsl = value;
                setting.SetBoolean("SmtpUseSsl", smtpUseSsl);
            }
        }
        
        private string fromMailAddress;
        private string toMailAddress;
        private string textEncoding;
        private string smtpServer;
        private int smtpPort;
        private string smtpUserName;
        private SecureString smtpPassword;
        private bool smtpUseSsl;
    }
}
